﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using rateITcd.Models;

namespace rateITcd.Controllers
{
    public class RestaurantDishController : Controller
    {
        private RateItDB db = new RateItDB();

        // GET: RestaurantDish
        public ActionResult Index()
        {
            // Only admins can see the list of all dishes
            if (User.IsInRole("Admin"))
            {
                return View(db.Dishes.ToList());
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        // GET: RestaurantDish/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            RestaurantDish restaurantDish = db.Dishes.Find(id);

            if (restaurantDish == null)
            {
                return HttpNotFound();
            }
            return View(restaurantDish);
        }

        // GET: RestaurantDish/Create
        public ActionResult Create(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            // Only Admins can add new dishes
            if (User.IsInRole("Admin"))
            {
                ViewBag.RestaurantId = id;
                return View();
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        // POST: RestaurantDish/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,Price")] RestaurantDish restaurantDish, int? restaurantId)
        {
            if (User.IsInRole("Admin"))
            {
                if (ModelState.IsValid)
                {
                    //db.Dishes.Add(restaurantDish);
                    Restaurant restaurant = db.Restaurants.Find(restaurantId);
                    restaurant.Dishes.Add(restaurantDish);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(restaurantDish);
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        // GET: RestaurantDish/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            RestaurantDish restaurantDish = db.Dishes.Find(id);
            if (User.IsInRole("Admin"))
            {

                if (restaurantDish == null)
                {
                    return HttpNotFound();
                }
                return View(restaurantDish);
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        // POST: RestaurantDish/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,Price")] RestaurantDish restaurantDish)
        {
            if (User.IsInRole("Admin"))
            {
                if (ModelState.IsValid)
                {
                    db.Entry(restaurantDish).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(restaurantDish);
            }
            else
            {
                return new HttpUnauthorizedResult();

            }
        }

        // GET: RestaurantDish/Delete/5
        public ActionResult Delete(int? id)
        {
            if (User.IsInRole("Admin"))
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                RestaurantDish restaurantDish = db.Dishes.Find(id);
                if (restaurantDish == null)
                {
                    return HttpNotFound();
                }
                return View(restaurantDish);
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        // POST: RestaurantDish/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            if (User.IsInRole("Admin"))
            {
                RestaurantDish restaurantDish = db.Dishes.Find(id);
                db.Dishes.Remove(restaurantDish);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
